package com.lottery.im.module.contact.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.module.tim.model.GroupFuture;
import com.lottery.im.util.transformations.CropCircleTransformation;
import com.tencent.imsdk.ext.group.TIMGroupPendencyGetType;
import com.tencent.imsdk.ext.group.TIMGroupPendencyItem;
import com.tencent.imsdk.ext.group.TIMGroupPendencyOperationType;
import java.util.List;

public class GroupMrgMsgAdapter extends BaseQuickAdapter<GroupFuture, BaseViewHolder> {

  public GroupMrgMsgAdapter(@Nullable List<GroupFuture> data) {
    super(R.layout.item_group_mrgmsg, data);
  }

  @Override protected void convert(BaseViewHolder helper, GroupFuture item) {

    final TIMGroupPendencyItem data = item.getFutureItem();
    String from = data.getFromUser(), to = data.getToUser();

    ImageView ivPortrait = helper.getView(R.id.iv_portrait);

    if (TextUtils.isEmpty(item.getFutureItem().getRequestMsg())) {
      helper.setGone(R.id.tv_summary, false);
    } else {
      helper.setGone(R.id.tv_summary, true);
      helper.setText(R.id.tv_summary, item.getFutureItem().getRequestMsg());
    }

    if (data.getPendencyType() == TIMGroupPendencyGetType.APPLY_BY_SELF) {
      // 申请入群
      helper.setText(R.id.tv_group_name, "申请加入 " + item.getGroupName());
      if (from.equals(MyApplication.getInstance().getThirdAccId())) {
        //自己申请加入群
        Glide.with(mContext)
            .load(MyApplication.getInstance().getUserInfo().getUserPic())
            .apply(new RequestOptions()
                .bitmapTransform(new CropCircleTransformation())
                .placeholder(R.drawable.icon_conversation_de)
                .error(R.drawable.icon_conversation_de))
            .into(ivPortrait);
        helper.setText(R.id.tv_name, "我");
      } else {
        //别人申请加入我的群
        Glide.with(mContext)
            .load(item.getAvatarUrl())
            .apply(new RequestOptions()
                .bitmapTransform(new CropCircleTransformation())
                .placeholder(R.drawable.icon_conversation_de)
                .error(R.drawable.icon_conversation_de))
            .into(ivPortrait);

        helper.setText(R.id.tv_name, item.getFromeUserName());
      }

      switch (item.getType()) {

        case HANDLED_BY_OTHER:
          break;
        case HANDLED_BY_SELF:
          // 已处理
          helper.setGone(R.id.btn_agree, false);
          helper.setGone(R.id.btn_refuse, false);
          helper.setGone(R.id.tv_status, true);
          if (item.getFutureItem().getOperationType() == TIMGroupPendencyOperationType.ACCEPT) {
            helper.setText(R.id.tv_status, "已同意");
          } else {
            helper.setText(R.id.tv_status, "已拒绝");
          }
          break;
        case NOT_HANDLED:
          // 未处理
          helper.setGone(R.id.btn_agree, true);
          helper.setGone(R.id.btn_refuse, true);
          helper.setGone(R.id.tv_status, false);
          break;
      }
    } else {
      if (to.equals(MyApplication.getInstance().getThirdAccId())) {
        //别人邀请我入群

        helper.setText(R.id.tv_name, item.getFromeUserName());
        helper.setText(R.id.tv_group_name,
            "邀请我加入 " + item.getGroupName());

        Glide.with(mContext)
            .load(item.getAvatarUrl())
            .apply(new RequestOptions()
                .bitmapTransform(new CropCircleTransformation())
                .placeholder(R.drawable.icon_conversation_de)
                .error(R.drawable.icon_conversation_de))
            .into(ivPortrait);
      } else {
        //邀请别人入群
        helper.setText(R.id.tv_name, "我");
        helper.setText(R.id.tv_group_name,
            "邀请" + item.getToUserName() + "加入 " + item.getGroupName());

        Glide.with(mContext)
            .load(MyApplication.getInstance().getUserInfo().getUserPic())
            .apply(new RequestOptions()
                .bitmapTransform(new CropCircleTransformation())
                .placeholder(R.drawable.icon_conversation_de)
                .error(R.drawable.icon_conversation_de))
            .into(ivPortrait);
      }
    }
    helper.addOnClickListener(R.id.btn_agree);
    helper.addOnClickListener(R.id.btn_refuse);
  }
}
